<template>
  <div class="app-container">
    <div class="order_stat">
      <div class="title">
        <span>积分商品</span>
        <!-- <router-link to="/pointsMall/goods/index1"> -->
        <el-button type="primary" size="mini" @click="handleAdd"
          >添加商品</el-button
        >
        <!-- </router-link> -->
      </div>
    </div>
    <div class="table">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="88px"
      >
        <el-form-item label="商品名称" prop="goodName">
          <el-input
            v-model="queryParams.goodName"
            placeholder="请输入商品名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <!-- <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['system:good:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['system:good:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                    @click="handleDelete" v-hasPermi="['system:good:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['system:good:export']">批量导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row> -->

      <el-table
        v-loading="loading"
        :data="goodList"
        @selection-change="handleSelectionChange"
      >
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column label="商品ID" align="center" prop="id" />
        <el-table-column label="商品" align="center" prop="goodName" />
        <el-table-column label="商品类型" align="center" prop="goodType">
          <template slot-scope="{ row }">
            {{ row.goodType == 1 ? "实体商品" : "虚拟商品" }}
          </template>
        </el-table-column>
        <el-table-column label="商品主图" align="center" prop="goodImg">
          <template slot-scope="{ row }">
            <image-preview
              :src="row.goodImg"
              width="50px"
              height="50px"
            ></image-preview>
          </template>
        </el-table-column>
        <el-table-column label="是否上架" align="center" prop="isInitiate">
          <template slot-scope="{ row }">
            {{ row.isInitiate == 1 ? "上架" : "下架" }}
          </template>
        </el-table-column>
        <el-table-column
          label="会员购买权限"
          align="center"
          prop="memberBuyingRights"
        >
          <template slot-scope="{ row }">
            <el-tag
              size="mini"
              style="margin-right: 5px"
              v-for="(item, index) in row.memberBayAuthority"
              :key="index"
              >{{ forrmatterEnmu(DictData.memberPurchase, item) }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="兑换类型" align="center" prop="redemptionSet">
          <template slot-scope="{ row }">
            {{ forrmatterEnmu(DictData.integralDeduction, row.redemptionSet) }}
          </template>
        </el-table-column>
        <el-table-column label="兑换价" align="center" prop="integralPrice">
        </el-table-column>
        <el-table-column label="兑换数量" align="center" prop="pointsUse">
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime">
        </el-table-column>
        <el-table-column
          width="180"
          label="备注"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                            v-hasPermi="['system:good:edit']">推广码</el-button> -->
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:good:edit']"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:good:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加或修改供应商商品对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1200px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="商品信息" prop="id">
          <el-button type="primary" plain @click="goodOpen = true"
            >选择商品</el-button
          >
          <div class="goods-container">
            <div
              class="goods-item"
              v-for="(item, index) in productList"
              :key="item.id"
            >
              <div class="goods-item-left">
                <img :src="baseURL + item.goodImg" alt="" />
                <div class="good-item-title">
                  <div>{{ item.goodName }}</div>
                  <div>￥ {{ item.retailPrice }}</div>
                </div>
                <el-button
                  icon="el-icon-delete"
                  plain
                  @click="goodsSelectionDelete(index)"
                  >删除</el-button
                >
              </div>
            </div>
          </div>
        </el-form-item>
        <!-- <el-form-item label="积分使用数量" prop="pointsUse">
                    <el-input v-model="form.pointsUse" placeholder="请输入积分使用数量" />
                </el-form-item>
                <el-form-item label="积分价格" prop="integralPrice">
                    <el-input v-model="form.integralPrice" placeholder="请输入积分价格" />
                </el-form-item> -->
        <el-form-item label="兑换条件" prop="redemptionSet" v-if="form.id">
          <el-radio v-model="form.redemptionSet" label="1">全积分兑换</el-radio>
          <el-radio v-model="form.redemptionSet" label="2"
            >积分+金额兑换</el-radio
          >
        </el-form-item>
        <el-form-item label="兑换价格" prop="pointsUse" v-if="form.id">
          <!-- <el-input v-model="form.pointsUse" placeholder="请输入积分价格" /> -->
          <el-table
            :data="convertList"
            border
            :header-cell-style="{ background: '#fff' }"
          >
            <el-table-column
              prop="specifications"
              label="规格"
            ></el-table-column>
            <el-table-column prop="price" label="售价">
              <template slot-scope="{ row }">
                {{ row.price ? `￥ ${row.price}` : "" }}
              </template>
            </el-table-column>
            <el-table-column prop="" label="兑换价">
              <template slot-scope="{ row }">
                <div class="convert">
                  <div class="convert">
                    <el-input
                      v-model="row.pointsUse"
                      size="mini"
                      clearable
                    ></el-input>
                    <div style="width: 40px; margin: 0 10px">积分</div>
                  </div>
                  <div class="convert" v-if="form.redemptionSet == 2">
                    +
                    <el-input
                      v-model="row.integralPrice"
                      size="mini"
                      clearable
                      style="margin: 0 10px"
                    ></el-input>
                    元
                  </div>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

      <!-- 选择商品列表 -->
      <el-dialog
        title="绑定会员等级商品"
        :visible.sync="goodOpen"
        width="800px"
        append-to-body
      >
        <GoodList @tableSelection="tableSelection"></GoodList>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
import {
  integralGoodList,
  integralGoodId,
  integralGoodAdd,
  integralGoodEdit,
  integralGoodDelete,
} from "@/api/pointsMall/goods";
import GoodList from "./components/goodList.vue";
import { forrmatterEnmu } from "@/utils/index";

export default {
  name: "Good",
  components: { GoodList },
  data() {
    return {
      order_module: [
        { name: "累计余额", num: 1939376.64, toolip: "累计所有平台产生的余额" },
        {
          name: "已使用",
          num: 1939376.64,
          toolip: "使用余额支付或使用余额抵扣统计",
        },
        {
          name: "会员账户余额",
          num: 1939376.64,
          toolip: "所有会员现有账户的余额",
        },
        { name: "已提现余额", num: 1939376.64, toolip: "已提现的账户累计余额" },
      ],
      checkboxList: "",
      activeName: "first",
      // 日期范围
      dateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商商品表格数据
      goodList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptName: null,
        sort: null,
        classifyImg: null,
        status: null,
        delFlag: null,
        goodName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: "商品信息不能为空", trigger: "blur" }],
        redemptionSet: [
          { required: true, message: "兑换条件不能为空", trigger: "blur" },
        ],
        pointsUse: [
          { required: true, message: "兑换价格不能为空", trigger: "blur" },
        ],
      },
      goodOpen: false,
      // 商品列表
      productList: [],
      baseURL: process.env.VUE_APP_BASE_API,
      convertList: [
        {
          specifications: "默认",
          price: "",
          pointsUse: null,
          integralPrice: null,
        },
      ],
      isAdd: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    forrmatterEnmu,
    /** 查询供应商商品列表 */
    getList() {
      this.loading = true;
      // this.addDateRange(this.queryParams, this.dateRange)
      integralGoodList(this.queryParams).then((response) => {
        this.goodList = response.rows.map((item) => {
          item.memberBayAuthority = item.memberBuyingRights?.split(",");
          return item;
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pointsUse: null,
        integralPrice: null,
        redemptionSet: "1",
      };
      this.productList = [];
      this.resetForm("form");
    },
    uploadImg(v) {
      this.form.classifyImg = v;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加积分商品";
      this.isAdd = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // 后端返回的数据使用的就是商品id，同时也是这一条数据的id
      this.reset();
      this.isAdd = false;
      const id = row.id || this.ids;
      integralGoodId(id).then((response) => {
        this.productList.push(response.data);
        this.form.id = response.data.id;
        this.form.redemptionSet = response.data.redemptionSet;
        this.convertList.splice(0, 1, {
          specifications: "默认",
          price: response.data.retailPrice,
          pointsUse: response.data.pointsUse,
          integralPrice: response.data.integralPrice,
        });
        this.open = true;
        this.title = "修改积分商品";
      });
    },
    /** 提交按钮 */
    submitForm(type) {
      this.form.pointsUse = this.convertList[0].pointsUse;
      this.form.integralPrice = this.convertList[0].integralPrice;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (!this.isAdd) {
            integralGoodEdit(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            integralGoodAdd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除数据？")
        .then(function () {
          return integralGoodDelete(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/good/export",
        {
          ...this.queryParams,
        },
        `good_${new Date().getTime()}.xlsx`
      );
    },
    // 选择商品
    tableSelection(v) {
      this.form.id = v.map((item) => item.id).join(",");
      this.productList = v;
      this.convertList[0].price = v[0].retailPrice;
      this.goodOpen = false;
    },
    // 删除选择的商品
    goodsSelectionDelete(index) {
      this.productList.splice(index, 1);
    },
  },
};
</script>
<style lang="less" scoped>
.app-container {
  width: 100%;
  height: 1005;
  background-color: #f8f8f8;

  .order_stat {
    background: #fff;
    padding: 10px 10px;
    margin-bottom: 10px;

    .title {
      font-size: 18px;
      font-weight: bold;
      display: flex;
      align-items: center;
      height: 40px;
      display: flex;
      justify-content: space-between;
    }
  }

  .table {
    width: 100%;
    background-color: #fff;
    padding: 15px 10px;
  }
}

.goods-container {
  .goods-item {
    height: 80px;
    width: 100%;
    margin-top: 20px;
    padding: 0 10px;
    border: 1px solid #c9c9c9;
    display: flex;
    border-radius: 10px;

    .goods-item-left {
      display: flex;
      align-items: center;
      width: 100%;
      height: 100%;

      > img {
        height: 60px;
        width: 60px;
      }

      .good-item-title {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        margin: 10px 20px;
      }
    }
  }
}

.convert {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
}
</style>
